from django.shortcuts import render

# Create your views here.



from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin

# GET /areas/
from rest_framework.viewsets import ViewSet, ReadOnlyModelViewSet


# class AreasViewSet(ReadOnlyModelViewSet):
#     # 返回所有省信息
#
#     pass
from rest_framework_extensions.cache.mixins import ListCacheResponseMixin, RetrieveCacheResponseMixin, \
    CacheResponseMixin

from areas.models import Area
from areas.serializers import AreaSerializer, SubAreaSerializer


# class AreasAPIView(ListCacheResponseMixin,ListAPIView):
#     # 根据id　返回其下属城市
#     serializer_class = AreaSerializer
#     def get_queryset(self):
#         return Area.objects.filter(parent=None)
#
#
# # GET /areas/(?P<pk>\d+)/
# class AreasViewSet_sons(RetrieveCacheResponseMixin,RetrieveAPIView):
#     serializer_class = SubAreaSerializer
#     queryset = Area.objects.all()

class AreaViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer